package com.chagee.store.operation.server.service.third;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.chagee.store.operation.server.feign.UserApi;
import com.chagee.store.operation.server.feign.request.PersonnelAuthRequest;
import com.chagee.store.operation.server.feign.request.UserRoleRequest;
import com.chagee.store.operation.server.feign.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 霸王功夫-blade-user服务
 * @Author : hk
 * @Date : 2024/12/6
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class BladeUserService {

	@Resource
	private UserApi userApi;

	/**
	 * 获取登录人信息
	 *
	 * @return user
	 */
	public BladeUser getUser() {
		BladeUser user = AuthUtil.getUser();
		if (Objects.isNull(user)) {
			throw new BizException(BizErrorCode.NOT_GTE_LOGIN_USER_INFO);
		}
		log.info("当前登录人信息：{}", JSONUtil.toJsonStr(user));
		return user;
	}

	public List<String> getUserStoreCodeList(Long userId) {
		log.info("通过登录人id：{}获取其门店编码信息开始，", userId);
		List<String> stringList = new ArrayList<>();
		try {
			log.info("userApi.userBindStoreList() called with parameters => userId = {}", userId);
			R<List<UserStoreListVO>> r = userApi.userBindStoreList(userId);
			log.info("userApi.userBindStoreList() ==> r = {} ", JSONUtil.toJsonStr(r));
			if (r != null && CollUtil.isNotEmpty(r.getData())) {
				List<String> collect = r.getData().stream().map(UserStoreListVO::getStoreCode).collect(Collectors.toList());
				stringList.addAll(collect);
			}
			log.info("通过登录人id：{}获取其门店编码信息结束，门店编码：{}", userId, stringList);
		} catch (Exception e) {
			log.error("获取其门店编码信息异常", e);
			throw new BizException(BizErrorCode.USER_STORE_CODE_NULL);
		}
		return stringList;
	}

	public User userInfoByPhone(String phone) {
		log.info("根据手机号或编码获取霸王功夫侧用户信息：{}", phone);
		User user = null;
		try {
			log.info("userApi.userInfoByPhone ==> req {}", phone);
			R<UserInfo> r = userApi.userInfoByPhone(phone);
			log.info("userApi.userInfoByPhone ==> r = {} ", JSONUtil.toJsonStr(r));

			if (r != null && r.isSuccess() && Objects.nonNull(r.getData()) && Objects.nonNull(r.getData().getUser())) {
				user = r.getData().getUser();
			}
		} catch (Exception e) {
			log.error("根据手机号或编码获取霸王功夫侧用户信息", e);
			throw new BizException(BizErrorCode.GET_USER_DETAIL_NULL);
		}
		return user;
	}

	public Boolean addUserRole(Long userId, String roleName) {
		UserRoleRequest request = new UserRoleRequest();
		request.setUserId(userId);
		request.setRoleName(roleName);
		try {
			log.info("userApi.addUserRole ==> req:{}", JSONUtil.toJsonStr(request));
			R<Boolean> r = userApi.addUserRole(request);
			log.info("userApi.addUserRole ==> r = {} ", JSONUtil.toJsonStr(r));

			if (r != null && r.isSuccess()) {
				return r.isSuccess();
			}
		} catch (Exception e) {
			log.error("根据用户ID配置权限失败", e);
			throw new BizException(BizErrorCode.GET_USER_DETAIL_NULL);
		}
		return false;
	}
}
